package com.macro.mall.tiny.modules.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.dto.PmsProductCateChildrenDTO;
import com.macro.mall.tiny.dto.PmsProductCategoryDTO;
import com.macro.mall.tiny.modules.pms.model.PmsProductCategory;
import com.macro.mall.tiny.modules.pms.mapper.PmsProductCategoryMapper;
import com.macro.mall.tiny.modules.pms.model.PmsProductCategoryAttributeRelation;
import com.macro.mall.tiny.modules.pms.service.PmsProductCategoryAttributeRelationService;
import com.macro.mall.tiny.modules.pms.service.PmsProductCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 产品分类 服务实现类
 * </p>
 *
 * @author macro
 * @since 2024-08-15
 */
@Service
public class PmsProductCategoryServiceImpl extends ServiceImpl<PmsProductCategoryMapper, PmsProductCategory> implements PmsProductCategoryService {
    @Autowired
    PmsProductCategoryAttributeRelationService pmsProductCategoryAttributeRelationService;

    @Autowired
    PmsProductCategoryMapper pmsProductCategoryMapper;

    @Override
    public Page<PmsProductCategory> list(Long parentId, Integer pageSize, Integer pageNum) {
        Page<PmsProductCategory> page = new Page<>(pageNum, pageSize);
        QueryWrapper<PmsProductCategory> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(PmsProductCategory::getParentId, parentId)
                .orderByDesc(PmsProductCategory::getSort);
        return this.page(page, wrapper);
    }

    @Override
    public boolean updateShowStatus(Long ids, Integer showStatus) {
        PmsProductCategory productCategory = new PmsProductCategory();
        productCategory.setId(ids);
        productCategory.setShowStatus(showStatus);
        return updateById(productCategory);
    }

    @Override
    public boolean updateNavStatus(Long ids, Integer navStatus) {
        PmsProductCategory productCategory = new PmsProductCategory();
        productCategory.setId(ids);
        productCategory.setNavStatus(navStatus);
        return updateById(productCategory);
    }

    @Override
    @Transactional
    public boolean create(PmsProductCategoryDTO pmsProductCategoryDTO) {
        pmsProductCategoryDTO.setProductCount(0);
        updateLevel(pmsProductCategoryDTO);
        save(pmsProductCategoryDTO);
        saveAttributeRelation(pmsProductCategoryDTO);
        return true;
    }

    @Override
    @Transactional
    public boolean update(PmsProductCategoryDTO pmsProductCategoryDTO) {
        updateLevel(pmsProductCategoryDTO);
        updateById(pmsProductCategoryDTO);
        QueryWrapper<PmsProductCategoryAttributeRelation> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(PmsProductCategoryAttributeRelation::getProductCategoryId, pmsProductCategoryDTO.getId());
        pmsProductCategoryAttributeRelationService.remove(wrapper);
        saveAttributeRelation(pmsProductCategoryDTO);
        return true;
    }

    @Override
    @Transactional
    public boolean delete(Long id) {
        removeById(id);
        QueryWrapper<PmsProductCategoryAttributeRelation> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(PmsProductCategoryAttributeRelation::getProductCategoryId, id);
        pmsProductCategoryAttributeRelationService.remove(wrapper);
        return true;
    }

    @Override
    public List<PmsProductCateChildrenDTO> listWithChildren() {
        return pmsProductCategoryMapper.listWithChildren();
    }

    private void updateLevel(PmsProductCategory productCategory) {
        if (productCategory.getParentId() == 0) {
            productCategory.setLevel(0);
        } else {
            PmsProductCategory parentCategory = getById(productCategory.getParentId());
            if (parentCategory != null) {
                productCategory.setLevel(parentCategory.getLevel() + 1);
            } else {
                productCategory.setLevel(0);
            }
        }
    }

    private boolean saveAttributeRelation(PmsProductCategoryDTO pmsProductCategoryDTO) {
        List<PmsProductCategoryAttributeRelation> relationList = new ArrayList<>();
        for (Long productAttrId : pmsProductCategoryDTO.getProductAttributeIdList()) {
            PmsProductCategoryAttributeRelation relation = new PmsProductCategoryAttributeRelation();
            relation.setProductAttributeId(productAttrId);
            relation.setProductCategoryId(pmsProductCategoryDTO.getId());
            relationList.add(relation);
        }
        return pmsProductCategoryAttributeRelationService.saveBatch(relationList);
    }
}
